package com.xmuexp109.exp5.service;

import com.xmuexp109.exp5.dao.ordersDao;
import com.xmuexp109.exp5.model.VoObject;
import com.xmuexp109.exp5.model.orders;
import com.xmuexp109.exp5.model.ordersPo;
import com.xmuexp109.exp5.model.ordersVo;
import com.xmuexp109.exp5.util.ResponseCode;
import com.xmuexp109.exp5.util.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ordersService {
    @Autowired
    private ordersDao ordersDao;


    public ReturnObject<VoObject> findById(Long id) {
        ordersPo queryObj = new ordersPo();
        queryObj.setId(id);
        ReturnObject<VoObject> retGoods = null;
        ReturnObject<List<orders>> returnObject = ordersDao.findOrders(queryObj, true);
        if (returnObject.getCode().equals(ResponseCode.OK)) {
            if (returnObject.getData().size() == 1) {
                retGoods = new ReturnObject<>(returnObject.getData().get(0));
            }else{
                retGoods = new ReturnObject<>(ResponseCode.RESOURCE_ID_NOTEXIST);
            }
        }else{
            retGoods = new ReturnObject<>(returnObject.getCode(), returnObject.getErrmsg());
        }
        return retGoods;
    }



    @Transactional
    public ReturnObject<VoObject> createOrders(ordersVo ordersVo) {
        orders orders = ordersVo.createOrders();
        ReturnObject<orders> retObj = ordersDao.createOrders(orders);
        ReturnObject<VoObject> retGoods = null;
        if (retObj.getCode().equals(ResponseCode.OK)) {
            retGoods = new ReturnObject<>(retObj.getData());
        }else{
            retGoods = new ReturnObject<>(retObj.getCode(), retObj.getErrmsg());
        }
        return retGoods;
    }
}
